package com.guigu.liuzhireng.controller;

import com.guigu.aop.OpLog;
import com.guigu.liuzhireng.pojo.Authorization;
import com.guigu.liuzhireng.pojo.Roleauthorization;
import com.guigu.liuzhireng.service.RoleauthorizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("Roleauthorization.do")
public class RoleauthorizationController {

    @Autowired
    RoleauthorizationService roleauthorizationService;

    @RequestMapping("RoleAuthAll")
    @ResponseBody
    public List<Authorization> RoleAuthAll(Integer roleid){
        return roleauthorizationService.RoleauthorizationAll(roleid);
    }

    @RequestMapping("DelAdd")
    @OpLog(opModule = "角色权限模块",operType = "角色赋予/收回权限",operDesc = "此方法用于角色赋予/收回权限")
    public String DelAdd(HttpServletRequest req){
        int roleid = Integer.parseInt(req.getParameter("roleid"));
        String[] authids = req.getParameterValues("auid");

        List<Roleauthorization> roleauthorizations = new ArrayList<>();
        for (String role : authids) {
            Roleauthorization roleauthorization = new Roleauthorization();
            roleauthorization.setAuid(Integer.parseInt(role));
            roleauthorization.setRoleid(roleid);
            roleauthorizations.add(roleauthorization);
        }

        roleauthorizationService.DelAdd(roleauthorizations,roleid);
        return "/role/Role.html";
    }
}
